//
//  GXAddContentController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/29.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXAddContentController.h"
#import "GXAddMenuController.h"
#import "GXAddContentCell.h"
#import "GXAddContentModel.h"
#import "GXMenuChangeController.h"
@interface GXAddContentController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * contentTableView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@end
static NSString *const addContentCell=@"GXAddContentCell";
@implementation GXAddContentController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.title = @"新添内容";
    [self creatAllViews];
    [self requestData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMenuList:) name:kNotificationAddMenu object:nil];
}
- (void)creatAllViews
{
    [self setUPNavigationRightItem];
    [self.view addSubview:self.contentTableView];
}
- (void)reloadMenuList:(NSNotification *)notifi
{
    MyLog(@"kNotificationAddMenu");
    [self requestData];
}
- (void)requestData
{
    self.dataArray = [NSMutableArray array];
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [MenuContentManager selectMenuListWithStoreID:storeId success:^(id responseObject) {
        [self handleMessageData:responseObject];
    } fail:^(id responseObject) {
    }];
}
- (void)handleMessageData:(id)responseObj
{
    NSDictionary *contentDic = responseObj[@"result"];
    if (![contentDic[@"services"] count]) {
        [SVProgressHUD showImage:nil status:@"无数据"];
    }else
    {
        for (NSDictionary * dict in contentDic[@"services"]) {
           GXAddContentModel * model = [GXAddContentModel mj_objectWithKeyValues:dict];
            [self.dataArray addObject:model];
        }
    }
    [self.contentTableView reloadData];
}

- (void)setUPNavigationRightItem
{
    UIButton * rightButton = [UIButton setButtonTitleNormal:@"新增" buttonTintColor:[UIColor whiteColor]];
    [rightButton addTarget:self action:@selector(clickRightItemButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}
- (void)clickRightItemButton:(UIButton *)bt
{
    GXAddMenuController * addMenuVC = [[GXAddMenuController alloc]init];
    [self.navigationController pushViewController:addMenuVC animated:YES];
}
#pragma mark -- 创建uitableview
- (UITableView *)contentTableView
{
    if (_contentTableView == nil) {
        _contentTableView =[[UITableView alloc] initWithFrame:CGRectMake(10,15,kUIScreenWidth-20,kUIScreenHeight-Height_TabBar-Height_StatusBar-30) style:UITableViewStyleGrouped];
        _contentTableView .delegate=self;
        _contentTableView .dataSource=self;
        _contentTableView .sectionHeaderHeight = 5;
        _contentTableView .sectionFooterHeight = 0.01;
        _contentTableView .showsHorizontalScrollIndicator=NO;
        _contentTableView .showsVerticalScrollIndicator=NO;
        _contentTableView .separatorStyle=UITableViewCellSeparatorStyleNone;
        _contentTableView .backgroundColor = [UIColor whiteColor];
        _contentTableView .layer.masksToBounds = YES;
        _contentTableView .layer.cornerRadius = 5;
        [_contentTableView  registerNib:[UINib nibWithNibName:@"GXAddContentCell" bundle:nil] forCellReuseIdentifier:addContentCell];
    }
    return _contentTableView;
}

#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GXAddContentCell * cell = [tableView dequeueReusableCellWithIdentifier:addContentCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    GXAddContentModel * model = self.dataArray[indexPath.section];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GXMenuChangeController *menuVC = [[GXMenuChangeController alloc]init];
    GXAddContentModel * model = self.dataArray[indexPath.section];
    menuVC.serviceId = model.serviceId;
    WeakSelf(weakSelf);
    menuVC.saveSuccessBlock = ^{
        [weakSelf requestData];
    };
    [self.navigationController pushViewController:menuVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
